package com.springemail.service.impl;

import com.springemail.service.MailServcie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * Created by BKWJA on 2018/10/29.
 */
@Service
public class MailServiceImpl implements MailServcie {

    //logger日志
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //邮件发送人，自己的邮箱地址
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendSimpleMail(String toUserMail, String subject, String content) {

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //发送给谁
        mailMessage.setTo(toUserMail);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        //邮件发送人
        mailMessage.setFrom(from);
        //通过JavaMailSender 将邮件发送出去
        mailSender.send(mailMessage);
    }

    @Override
    public void sendHtmlMail(String toUserMail, String subject, String content) {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

            helper.setTo(toUserMail);
            helper.setSubject(subject);
            //true：支持HTML发送邮件
            helper.setText(content,true);
            helper.setFrom(from);
            mailSender.send(mimeMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendAttachmentsMail(String toUserMail, String subject, String content, String filePath) {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setTo(toUserMail);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setFrom(from);

            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            //获取文件名
            String pathName = fileSystemResource.getFilename();
            helper.addAttachment(pathName,fileSystemResource);
            helper.addAttachment(pathName+"_test",fileSystemResource); //同时发送多个附件
            mailSender.send(mimeMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void sendInlineResoureMail(String toUserMail, String subject, String content,
                                      String rscPath, String rscId) {
        logger.info("发送带图片的HTML邮件开始：{},{},{},{},{}",toUserMail,subject,content,rscPath,rscId);
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

            helper.setFrom(from);
            helper.setTo(toUserMail);
            helper.setSubject(subject);
            helper.setText(content,true);

            //获取上传的文件路径
            FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId,fileSystemResource);
            helper.addInline(rscId,fileSystemResource);
            mailSender.send(mimeMessage);
            logger.info("发送带图片的HTML邮件成功！");

        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("发送带图片的HTML邮件失败：",e);
        }

    }
}
